package com.example.sms.receiver;

import com.example.common.util.constant.CommonUtilConstants;
import com.example.common.util.utils.StringUtils;
import com.example.model.vo.sms.SmsVo;
import com.example.service.util.utils.MailTemplateUtils;
import com.example.sms.service.SmsService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @Author juzipi
 * @Date 2021/7/27 16:21
 * @Info
 */
@Component
public class SmsReceiver {


    @Autowired
    private SmsService smsService;


    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    //监听sms.queue队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = CommonUtilConstants.SMS_QUEUE, durable = "true"),
            exchange = @Exchange(value = CommonUtilConstants.SMS_EXCHANGE),
            key = {CommonUtilConstants.SMS_ROUTING}
    ))
    public void smsConsumer(SmsVo smsVo, Channel channel, Message message) throws IOException {
        try {
            //消费掉消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
            logger.info("消息已确认 -------------> "+smsVo.getTemplateCode());
            if (StringUtils.isNotEmpty(smsVo.getPhone())){
                Boolean send = smsService.send(smsVo);
            }
        } catch (IOException e) {
            e.printStackTrace();
            //消费失败,此条消息重新回到队列
            logger.error("消息未成功消费,返回消息队列 -------------> "+e.getMessage());
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }

    }



    //监听mail.queue队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = CommonUtilConstants.MAIL_QUEUE, durable = "true"),
            exchange = @Exchange(value = CommonUtilConstants.MAIL_EXCHANGE),
            key = {CommonUtilConstants.MAIL_ROUTING}
    ))
    public void mailConsumer(SmsVo smsVo, Channel channel, Message message) throws IOException {
        try {
            //消费掉消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
            logger.info("消息已确认 -------------> "+smsVo.getPhone());
            //暂时用邮件代替手机信息，因为模板申请不到
            if (Integer.parseInt(smsVo.getTemplateCode()) == 0){
                String template = MailTemplateUtils.patientMailTemplate(smsVo.getParam());
                Boolean send = smsService.sendEmail("就诊通知",template);
            }
            if (Integer.parseInt(smsVo.getTemplateCode()) == 1 || Integer.parseInt(smsVo.getTemplateCode()) == 2){
                String template = MailTemplateUtils.reserveRenewMailTemplate(smsVo.getParam());
                Boolean aBoolean = smsService.sendEmail("预约更新通知", template);
            }
        } catch (IOException e) {
            e.printStackTrace();
            //消费失败,此条消息重新回到队列
            logger.error("消息未成功消费,返回消息队列 -------------> "+e.getMessage());
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        }

    }




}
